// pages/reg-agency/reg-agency.js
const SERVICE = require("../../service.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isDisabled: false,
    showLoadingIcon:false,
    loadingtext:"获取验证码",
    getVerificationCode: 60,
    isDisabledMobile: false,
    yzm_val:0,
    name_val:0,
    testMobile:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   *@Method: mobile 手机号 
   * 
   */
  mobile(e) {
    let mobile_val = e.detail.value;
    if (checkPhone(mobile_val) === false && mobile_val.length > 0) {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: "none",
        mask: true
      });
    }else{
     mobile_val
    }
  },
  inputMobile(e) {
    let mobile_val = e.detail.value;
    if (checkPhone(mobile_val) !== false) {
      this.setData({
        testMobile:true,
        mobile_val
      });
    } else {
      this.setData({
        testMobile:false,
        mobile_val
      });
    }
  },
  getVerificationCode(e) {
    let _this = this, td = this.data, phone = td.mobile_val;
    console.log(phone);
    let openid = wx.getStorageSync("token");    
    
    if (!phone){
      wx.showToast({
        title: "请输入手机号",
        icon: "none",
        mask: true
      });
    } else if (checkPhone(phone) == false){
      wx.showToast({
        title: "请输入正确的手机号",
        icon: "none",
        mask: true
      });
   }else{
      _this.setData({
        showLoadingIcon: true,
        loadingtext: "获取中...",
        isDisabled: true,
        isDisabledMobile: true
      });
     SERVICE.getVerificationCode({ phone, openid }, (status, data) => {
       console.log(data);
       if (status) {
         let intler = setInterval(callback => {
           td.getVerificationCode--;
           _this.setData({
             loadingtext: '倒计时'+td.getVerificationCode + 's',
             showLoadingIcon: false
           });

           if (td.getVerificationCode == 0) {
             clearInterval(intler);
             _this.setData({
               getVerificationCode: 60,
               loadingtext: "获取验证码",
               isDisabled: false,
               isDisabledMobile: false
             });
           }
         }, 1000);
       } else {
         _this.setData({
           loadingtext: "获取验证码",
           isDisabledMobile: false,
           showLoadingIcon: false
         });
         wx.showToast({
           title: data.message,
           icon: "none",
           mask: true
         });
       }
     });
   }
  },
  inputYzm(e){
    let yzm_val = e.detail.value;
    if (yzm_val.length == 6){
      this.setData({
        yzm_val
      });
    }else{
      this.setData({
        yzm_val:0
      });
    }
  },
  inputName(e) {
    let name_val = e.detail.value;
    if (name_val.length > 0) {
      this.setData({
        name_val
      });
    }else{
      this.setData({
        name_val:0
      });
    }
  },
  formSubmit(e){
    let mobile = e.detail.value.mobile,
      name = e.detail.value.name,
      yzm = e.detail.value.yzm;
      console.log(yzm);
    let openid = wx.getStorageSync("token");
    
    if (!mobile){
      wx.showToast({
        title: "请输入手机号",
        icon: "none",
        mask: true
      });
    } else if (checkPhone(mobile) == false) {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: "none",
        mask: true
      });
    } else if (yzm == 0 || yzm == ""){
      wx.showToast({
        title: "请输入验证码",
        icon: "none",
        mask: true
      });
    } else if (name == 0 || name == ""){
      wx.showToast({
        title: "请输入真实姓名",
        icon: "none",
        mask: true
      });
    }else{
        SERVICE.regAgency({
        "name": name,
        "phone": mobile,
        "code":yzm,
          openid,
      },(status,data)=>{
        wx.showToast({
          title:data.message,
          icon: "none",
          mask: true
        });
        if(status){
          setTimeout(() => {
            wx.navigateBack();
          }, 1000);
        }
      });
    }
  
  }
})

// 检验手机号
function checkPhone(val) {
  if (!(/^1[34578]\d{9}$/.test(val))) {
    return false;
  }
}    
